package wu.dao;

import org.apache.ibatis.session.SqlSession;
import wu.mapper.CreateTableMapper;
import wu.mapper.UserNiuMapper;
import wu.pojo.User_niu;
import wu.util.MyBatisUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/*
写一个，成绩展示研发中心的周赛静态网站，网站有
1.全部研发中心成员成绩表格显示
2.能点名字进入的该同学的最近几次比赛的成绩情况变化（带折线图）
3.带筛选班级，年级，Ac数的下拉筛选框
4.可以搜索名字
5.有导出当前表格数据的按钮
6.最近几次的比赛研发中心的平均排名、参与人数、平均Ac数、平均成绩 的折线图
7.可以上传成绩文件，文件格式为txt，文件内容为：学号 姓名 班级 年级 成绩 Ac数
* */


public class Run_Insert {
    public static String content = "";//先粘贴元素 <div class="rank-main clearfix"> *** </div>
    public static void main(String[] args) {
//        txtInsertAll();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要插入的周赛周数：");
//        int num =60;
        int num = sc.nextInt();
        StringInsertAll(num);
    }
    public static void txtInsertAll() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要插入的周赛周数：");
        int num = sc.nextInt();
        testInsertAll(num);
    }
    public static void StringInsertAll(int num) {

        //创建文件***************************
        String path = "src/main/resources/niu_number_txtfile/niu_" + num + "_txt";
        try {
            File file = new File(path);
            if (!file.exists()) {
                if (file.createNewFile()) {
                    System.out.println("File created: " + file.getName());
                } else {
                    System.out.println("Failed to create file.");
                }
            }

            FileWriter writer = new FileWriter(file);
            writer.write(content);
            writer.close();

            System.out.println("Successfully wrote to the file.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
        //************************************************************

        testInsertAll(num);
    }

    public static void testInsertAll(int num) {
        SqlSession sqlSession = MyBatisUtils.getSession();
        List<String> readlist = ReadTxtDao.readfile(num);

        System.out.println("输出读取的txt文件内容  ****************************************");
        for (String content : readlist) {
            System.out.println(content);
        }
        System.out.println("输出完成        *********************************************");

        System.out.println("传入getUser");
        List<User_niu> list = ReadTxtDao.getniu_User(num, readlist);
        System.out.println("输出获取的list  *********************************************");
        for (User_niu user_niu : list) {
            System.out.println(user_niu.toString());
        }
        System.out.println("输出完成        *********************************************");

        System.out.println("正在建表");
        Map<String, String> params = new HashMap<>();
        params.put("tableName", ("user_table_" + num));
//        params.put("table_name", "wu666");
        CreateTableMapper createTableMapper = sqlSession.getMapper(CreateTableMapper.class);
        createTableMapper.createtable222(params);
        System.out.println("建表完成");

        System.out.println("正在插入数据");
        UserNiuMapper mapper = sqlSession.getMapper(UserNiuMapper.class);
        mapper.insertUserNiuList("user_table_" + num, list);
        System.out.println("插入完成");
        sqlSession.commit();
        sqlSession.close();
    }
}
